package com.jorado.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimeZone;

/**
 * Created by len.zhang on 2015/10/2.
 */
public final class JsonUtils {

    private static Logger LOGGER = LoggerFactory.getLogger(JsonUtils.class);

    private JsonUtils() {
    }

    private static final ObjectMapper MAPPER = new ObjectMapper();
    //private static final PropertyNamingStrategy IGNORE_CASE = new IgnorePropertyNamingStrategy();

    static {
        MAPPER
                .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                .setTimeZone(TimeZone.getDefault());
        // .setPropertyNamingStrategy(IGNORE_CASE);
    }

    public static <T> T fromJson(String value, Class<T> cls) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            return MAPPER.readValue(value, cls);
        } catch (Exception ex) {
            LOGGER.error("from json error:" + ex.getMessage(), ex);
            return null;
        }
    }

    public static <T> T fromJson(String value, TypeReference<T> trf) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            return MAPPER.readValue(value, trf);
        } catch (Exception ex) {
            LOGGER.error("from json error:" + ex.getMessage(), ex);
            return null;
        }
    }

    public static <T> String toJson(T obj) {
        if (null == obj) {
            return "[]";
        }
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (Exception ex) {
            LOGGER.error("to json error:" + ex.getMessage(), ex);
            return "[]";
        }
    }

    public static <T> String toPrettyJson(T value) {
        try {
            return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(value);
        } catch (Exception ex) {
            LOGGER.error("to json error:" + ex.getMessage(), ex);
            return null;
        }
    }

//    /**
//     * 忽略命名规则，按照原始属性名输出
//     */
//    private static class IgnorePropertyNamingStrategy extends PropertyNamingStrategy {
//
//        public IgnorePropertyNamingStrategy() {
//
//        }
//
//        @Override
//        public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
//            return field.getName();
//        }
//
//        @Override
//        public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
//            return method.getName().substring(3);
//        }
//
//        @Override
//        public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
//            return method.getName().substring(3);
//        }
//    }
}
